package cn.sxt.持久化topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/6/10 15:08
 */
public class TestTopicConsumer {
    //地址在mq conf activemq.xml里面找
    private static final String BROKER_URL = "tcp://39.99.166.61:61616";
    private static final String TOPIC_NAME = "mq-topic-test";

    public static void main(String[] args) throws JMSException, IOException {
        System.out.println("z3");

        ConnectionFactory factory = new ActiveMQConnectionFactory(BROKER_URL);

        Connection connection = factory.createConnection();
        // 类似 关注公众号
        connection.setClientID("z3");

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Topic topic = session.createTopic(TOPIC_NAME);

        TopicSubscriber topicSubscriber = session.createDurableSubscriber(topic, "remark");

        connection.start();

        Message message = topicSubscriber.receive();

        while (message != null) {
            TextMessage textMessage = (TextMessage) message;

            System.out.println(textMessage.getText());
            message = topicSubscriber.receive();
        }
        connection.close();
        topicSubscriber.close();
        session.close();


    }
}
